package pers.xx.sbm.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import pers.xx.sbm.dao.ReportDataCountyMapper;
import pers.xx.sbm.vo.AreaVO;

import javax.servlet.http.HttpServletRequest;
import java.util.Calendar;
import java.util.List;

/**
 * @author ：xiexing
 * @description：报告控制器
 * @date ：2021/12/10 17:36
 */
@Controller
@RequestMapping("/report")
public class ReportController {

    @Autowired
    ReportDataCountyMapper reportDataCountyMapper;


    /**
     * @description 各个报告进入入口
     * @author XieXing
     * @date 2022/1/7 9:39
     */
    @RequestMapping("/reportEntrance")
    public String reportEntrance(Model model) {
        Calendar data = Calendar.getInstance();
        model.addAttribute("year", String.valueOf(data.get(Calendar.YEAR)-1));
        return "page/report/reportEntrance";
    }

    /**
     * @description 跳转到市级报告
     * @author XieXing
     * @date 2021/12/13 10:21
     */
    @RequestMapping("/toCityReport")
    public String toCityReport(Model model, HttpServletRequest request) {
        Calendar data = Calendar.getInstance();
        model.addAttribute("year", String.valueOf(data.get(Calendar.YEAR)-1));
        return "page/report/city_report";
    }

    /**
     * @description 区县（市）报告入口
     * @author XieXing
     * @date 2021/12/14 16:19
     */
    @RequestMapping("/countyReportEntrance")
    public String countyReportEntrance(Model model) {
        entranceCommonModel(model);
        return "page/report/county_report_entrance";
    }

    /**
     * @description 跳转到区县（市）报告
     * @author XieXing
     * @date 2021/12/13 10:21
     */
    @RequestMapping("/toCountyReport")
    public String toCountyReport(Model model, @RequestParam(defaultValue = "市属") String name, @RequestParam(defaultValue = "5201") String sysCode) {
        commonModel(name, sysCode, model);
        return "page/report/county_report";
    }

    /**
     * @description 跳转至德育市级报告
     * @author XieXing
     * @date 2021/12/30 14:50
     */
    @RequestMapping("/toMoralEducationCityReport")
    public String toMoralEducationCityReport(Model model) {
        commonModel("贵阳市", "520", model);
        return "page/report/moral_education_city_report";
    }


    /**
     * @description 跳转至德育区县报告
     * @author XieXing
     * @date 2021/12/30 14:50
     */
    @RequestMapping("/toMoralEducationCountyReport")
    public String toMoralEducationCountyReport(String name, String sysCode, Model model) {
        commonModel(name, sysCode, model);
        return "page/report/moral_education_county_report";
    }

    /**
     * @description 德育区县（市）报告入口
     * @author XieXing
     * @date 2021/12/14 16:19
     */
    @RequestMapping("/countyMoralReportEntrance")
    public String countyMoralReportEntrance(Model model) {
        entranceCommonModel(model);
        return "page/report/county_moral_report_entrance";
    }

    /**
     * @description 入口公共model设置
     * @author XieXing
     * @date 2022/1/7 9:34
     */
    private void entranceCommonModel(Model model) {
        Calendar data = Calendar.getInstance();
        model.addAttribute("year", String.valueOf(data.get(Calendar.YEAR)-1));
        List<AreaVO> areaVOList = reportDataCountyMapper.getArea();
        model.addAttribute("areaVOList", areaVOList);
    }

    /**
     * @description 公共跳转
     * @author XieXing
     * @date 2021/12/30 14:54
     */
    private void commonModel(String name, String sysCode, Model model) {
        Calendar data = Calendar.getInstance();
        int year = data.get(Calendar.YEAR)-1;
        int month = data.get(Calendar.MONTH) + 1;
        int beginYear = year - 2;//比对开始年份
        model.addAttribute("year", year + "");
        model.addAttribute("preYear", (year - 1) + "");
        model.addAttribute("month", month);
        model.addAttribute("beginYear", String.valueOf(beginYear));
        model.addAttribute("name", name);
        model.addAttribute("sysCode", sysCode);
        List<AreaVO> areaVOList = reportDataCountyMapper.getArea();
        model.addAttribute("areaVOList", areaVOList);
    }


}
